using System;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;

namespace BlogBackUp
{
    class HtmlUtils
    {
        public static ArrayList GetAdressesByDefaultHtmlDocument(HtmlDocument htmlDoc, String domain, String userName)
        {
            ArrayList allAdresses = new ArrayList();
            //StringCollection allAdresses = new StringCollection();
            

            String htmlFile = FindHtmlFile(htmlDoc);
            //String htmlFile = FindContentHtmlFile(htmlDoc);
            String[] elements = htmlFile.Split(new char[] {'\"'});
            String subAdress = domain + userName + "/" + "archive/";
            foreach(String element in elements)
            {
                int index = element.IndexOf(subAdress);
                if (index >= 0 && !allAdresses.Contains(element) && element.LastIndexOf(".html") == element.Length - 5)
                {
                    allAdresses.Add(element);
                }
            }            
            return allAdresses;
        }


        // all html file string
        public static String FindHtmlFile(HtmlDocument document)
        {
            String tempHtmlFile = "";
            foreach (HtmlElement element in document.All)
            {
                if (element.TagName.ToLower() == "html")
                {
                    tempHtmlFile = element.OuterHtml;
                    break;
                }
            }
            return tempHtmlFile;
        }

        // only the real useful content,this is only sutiable for my cppblog       
        // for other blogs, need to rewrite the function or change the function
        public static String FindContentHtmlFile(HtmlDocument document)
        {
            String tempHtmlFile = "";
            String headHtml = "";
            String contentHtml = "";

            foreach (HtmlElement e in document.All)
            {
                if (e.TagName.ToLower() == "head")
                {
                    // find head
                    headHtml = e.OuterHtml;
                }
                else if (e.TagName.ToLower() == "div")
                {
                    // find main html 
                    String tempContentHtml = "";
                    tempContentHtml = e.OuterHtml;
                    String subString = tempContentHtml.Substring(0, tempContentHtml.IndexOf('>') + 1);
                    if (subString.ToLower().IndexOf("class=postbody") > 0) 
                    {
                        contentHtml = tempContentHtml;
                    }
                }
            }
            // construct the real html file
            if (headHtml != "" && contentHtml != "")
            {
                tempHtmlFile += @"<html>";                
                tempHtmlFile += headHtml;               
                tempHtmlFile += @"<body>";               
                tempHtmlFile += contentHtml;               
                tempHtmlFile += @"</body>";               
                tempHtmlFile += @"</html>";
            }
            return tempHtmlFile;
        }

        public static void GetImgFileList(HtmlDocument document, String refFilesFullDirectory, ref ArrayList refFileList)
        {            
            String url = document.Url.ToString();
            foreach (HtmlElement element in document.Images)
            {
                // get the img's path
                String imgSourceUrl = "";
                String imgSourceFullUrl = "";
                String imgElementStr = element.OuterHtml;

                string[] attsStr = imgElementStr.Split(' ');
                foreach (string attStr in attsStr)
                {
                    if (attStr.IndexOf("src") == 0)
                    {
                        int index = attStr.IndexOf('\"');
                        int index2 = attStr.IndexOf('\"', index + 1);
                        imgSourceUrl = attStr.Substring(index + 1, index2 - index - 1);
                        break;
                    }
                }

                if (!imgSourceUrl.StartsWith("http://"))
                {
                    imgSourceFullUrl = imgSourceUrl.Insert(0, url);
                }
                else
                {
                    imgSourceFullUrl = imgSourceUrl;
                }

                int index3 = imgSourceUrl.LastIndexOf('/');
                String imgname = imgSourceUrl.Substring(index3 + 1, imgSourceUrl.Length - index3 - 1);
                String imgDestPath = refFilesFullDirectory + imgname;
                String ext = "";
                int index4 = imgname.LastIndexOf('.');
                if (index4 < 0)
                    ext = "";
                else
                    ext = imgname.Substring(index4);

                if (CommonUtils.IsImg(ext))
                {
                    RefFile imgFile = new RefFile();
                    imgFile.sourceUrl = imgSourceUrl;
                    imgFile.sourceFullUrl = imgSourceFullUrl;
                    imgFile.destPath = imgDestPath;
                    imgFile.fileName = imgname;
                    imgFile.ext = ext;

                    // sample: 
                    // sourceUrl = /intl/en_ALL/images/logo.gif
                    // sourceFullUrl = www.cppblog.com/mzty/intl/en_ALL/images/logo.gif
                    // destPath = reffile\logo.gif
                    // fileName = logo.gif
                    // ext = .gif               

                    refFileList.Add(imgFile);
                }
            }            
        }
        public static void GetCssFile(String htmlFile, String refFilesFullDirectory, ref RefFile cssFile)
        {
            int index = htmlFile.IndexOf("MainCss");
            int index2 = htmlFile.IndexOf('>', index);
            String mainCss = htmlFile.Substring(index, index2 - index);
            int index3 = 0;
            int index4 = 0;

            String cssSourceUrl = "";
            while (index3 < mainCss.Length && index4 < mainCss.Length)
            {
                index3 = mainCss.IndexOf('\"');
                index4 = mainCss.IndexOf('\"', index3 + 1);
                if (mainCss.Substring(index3, index4 - index3).ToLower().IndexOf(".css") > 0)
                {
                    cssSourceUrl = mainCss.Substring(index3 + 1, index4 - index3 - 1);
                    break;
                }
            }
            if (cssSourceUrl != "")
            {
                String cssFileName = cssSourceUrl.Substring(cssSourceUrl.LastIndexOf('/') + 1);                
                cssFile.sourceUrl = cssSourceUrl;
                cssFile.sourceFullUrl = cssSourceUrl;
                cssFile.destPath = refFilesFullDirectory + cssFileName;
                cssFile.fileName = cssFileName;
                cssFile.ext = cssFileName.Substring(cssFileName.LastIndexOf('.'));                
            }           
        }

    }
}